#include "doublelist.h"
#include "font.h"
#include "showbmp.h"
#include "ts.h"
#include "spro.h"
#include "fun.h"

struct LcdDevice *init_lcd(const char *device)
{
	//申请空间
	struct LcdDevice* lcd = malloc(sizeof(struct LcdDevice));
	if(lcd == NULL)
	{
		return NULL;
	} 

	//1打开设备
	lcd->fd = open(device, O_RDWR);
	if(lcd->fd < 0)
	{
		perror("open lcd fail");
		free(lcd);
		return NULL;
	}
	
	//映射
	lcd->mp = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd->fd,0);

	return lcd;
}




//定义变量存放坐标
int ts_x,ts_y;
int n=MAINBUY;
//主界面购买功能
int buy()
{
	//标识位，记录当前主页面刷了多少次商品节点
	int sum=0;
	int flag=0;
	int t;
	//主界面初始化
	show_anybmp(800,480,0,0,"main_menu.bmp");//背景
	struct doublelist *head=list_init();
	struct doublelist *p=head;
	struct doublelist *q=head;
	readlist(head);
	printf("==============现有商品==============\n");
	t=list_show(head);//记录链表除去头节点长度
	for(int i=0;i<4;i++)//展示初始商品
	{
		if(i>t-1)
		{
			break;
		}
		p=p->next;q=q->next;
		sum++;
		show_anybmp(75,120,75+160*i,50,p->picpath);
		//初始化Lcd
		struct LcdDevice* lcd = init_lcd("/dev/fb0");	
		//打开字体	
		font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
		//字体大小的设置
		fontSetSize(f,20);  //字体大小20，可以人为修改
		//创建一个画板（点阵图）宽是110个像素，高是120个像素
		bitmap *bm = createBitmapWithInit(110,120,4,getColor(0,255,255,255)); 
		char buf1[]="商品：";
		char buf2[]="价格：";
		char buf3[]="数量：";
		char name[100];
		char price[100];
		char num[100];
		sprintf(name,"%s",p->name);
		sprintf(price,"%.1f",p->price);
		sprintf(num,"%d",p->num);
		//将字体写到点阵图上 0,0表示汉字在画板的左上角显示
		fontPrint(f,bm,0,0,buf1,getColor(0,255,0,0),0);
		fontPrint(f,bm,0,40,buf2,getColor(0,255,0,0),0);
		fontPrint(f,bm,0,80,buf3,getColor(0,255,0,0),0);			
		fontPrint(f,bm,50,0,name,getColor(0,255,0,0),0);
		fontPrint(f,bm,50,40,price,getColor(0,255,0,0),0);
		fontPrint(f,bm,50,80,num,getColor(0,255,0,0),0);				
		//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
		show_font_to_lcd(lcd->mp,75+160*i,170,bm);
		//关闭字体，关闭画板
		fontUnload(f);
		destroyBitmap(bm);
		
	}
	//打开触摸屏
	ts_open();
	while(1) //读取触摸屏的坐标，判断你点击是哪个按钮
	{
		//判断坐标
		ts_read_pos(&ts_x,&ts_y);
		
		//管理员按钮
		if(ts_x>678 && ts_x<790 && ts_y>22 && ts_y<104) 
		{
			n=LOGIN;
			break;
		}
		
		//点击下一页
		if(ts_x>620 && ts_x<777 && ts_y>356 && ts_y<426)
		{
			if(p->next!=NULL)
			{
				sum=0;
				show_anybmp(800,480,0,0,"main_menu.bmp");
			}
			for(int i=0;i<4;i++)
			{
				if(p->next==NULL)
				{
					printf("已经是最后一页\n");
					break;
				}
					
				p=p->next;q=q->next;
				sum++;
				show_anybmp(75,120,75+160*i,50,p->picpath);//刷图片
				//初始化Lcd
				struct LcdDevice* lcd = init_lcd("/dev/fb0");	
				//打开字体	
				font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
				//字体大小的设置
				fontSetSize(f,20);  //字体大小20，可以人为修改
				//创建一个画板（点阵图）宽是200个像素，高是50个像素
				bitmap *bm = createBitmapWithInit(110,120,4,getColor(0,255,255,255)); 
				char buf1[]="商品：";
				char buf2[]="价格：";
				char buf3[]="数量：";
				char name[100];
				char price[100];
				char num[100];
				sprintf(name,"%s",p->name);
				sprintf(price,"%.1f",p->price);
				sprintf(num,"%d",p->num);
				//将字体写到点阵图上 0,0表示汉字在画板的左上角显示
				fontPrint(f,bm,0,0,buf1,getColor(0,255,0,0),0);
				fontPrint(f,bm,0,40,buf2,getColor(0,255,0,0),0);
				fontPrint(f,bm,0,80,buf3,getColor(0,255,0,0),0);			
				fontPrint(f,bm,50,0,name,getColor(0,255,0,0),0);
				fontPrint(f,bm,50,40,price,getColor(0,255,0,0),0);
				fontPrint(f,bm,50,80,num,getColor(0,255,0,0),0);				
				//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
				show_font_to_lcd(lcd->mp,75+160*i,170,bm);
				//关闭字体，关闭画板
				fontUnload(f);
				destroyBitmap(bm);
					
			}
			
		}
		//点击上一页
		if(ts_x>20 && ts_x<177 && ts_y>361 && ts_y<433)
		{
			
			for(int i=0;i<4+sum;i++)
			{
				if(p->prev==NULL)
				{
					printf("已经是第一页\n");
					break;
				}	
				p=p->prev;q=q->prev;
				
			}
			sum=0;
			for(int i=0;i<4;i++)
			{
				if(i>t-1)
				{
					break;
				}

				p=p->next;q=q->next;
				sum++;
				//刷图片
				show_anybmp(75,120,75+160*i,50,p->picpath);
				//初始化Lcd
				struct LcdDevice* lcd = init_lcd("/dev/fb0");	
				//打开字体	
				font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
				//字体大小的设置
				fontSetSize(f,20);  //字体大小20，可以人为修改
				//创建一个画板（点阵图）宽是200个像素，高是50个像素
				bitmap *bm = createBitmapWithInit(110,120,4,getColor(0,255,255,255)); 
				char buf1[]="商品：";
				char buf2[]="价格：";
				char buf3[]="数量：";
				char name[50];
				char price[50];
				char num[50];
				sprintf(name,"%s",p->name);
				sprintf(price,"%.1f",p->price);
				sprintf(num,"%d",p->num);
				//将字体写到点阵图上 0,0表示汉字在画板的左上角显示
				fontPrint(f,bm,0,0,buf1,getColor(0,255,0,0),0);
				fontPrint(f,bm,0,40,buf2,getColor(0,255,0,0),0);
				fontPrint(f,bm,0,80,buf3,getColor(0,255,0,0),0);			
				fontPrint(f,bm,50,0,name,getColor(0,255,0,0),0);
				fontPrint(f,bm,50,40,price,getColor(0,255,0,0),0);
				fontPrint(f,bm,50,80,num,getColor(0,255,0,0),0);				
				//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
				show_font_to_lcd(lcd->mp,75+160*i,170,bm);
				//关闭字体，关闭画板
				fontUnload(f);
				destroyBitmap(bm);
				
			}
		}
		//购物车结账
		if(ts_x>313 && ts_x<487 && ts_y>330 && ts_y<474)//点到购买按钮
		{
			lb1:
			printf("结账\n");
			show_anybmp(800,480,0,0,"pay.bmp");
			
			show_anybmp(75,120,265,20,q->picpath);
			//初始化Lcd
			struct LcdDevice* lcd = init_lcd("/dev/fb0");	
			//打开字体	
			font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
			//字体大小的设置
			fontSetSize(f,20);  //字体大小20，可以人为修改
			//创建一个画板（点阵图）宽是200个像素，高是50个像素
			bitmap *bm = createBitmapWithInit(110,120,4,getColor(0,255,255,255)); 
			char buf1[]="商品：";
			char buf2[]="价格：";
			char buf3[]="数量：";
			char name[50];
			char price[50];
			char num[50];
			sprintf(name,"%s",q->name);
			sprintf(price,"%.1f",q->price);
			sprintf(num,"%d",q->num);
			//将字体写到点阵图上 0,0表示汉字在画板的左上角显示
			fontPrint(f,bm,0,0,buf1,getColor(0,255,0,0),0);
			fontPrint(f,bm,0,40,buf2,getColor(0,255,0,0),0);
			fontPrint(f,bm,0,80,buf3,getColor(0,255,0,0),0);			
			fontPrint(f,bm,50,0,name,getColor(0,255,0,0),0);
			fontPrint(f,bm,50,40,price,getColor(0,255,0,0),0);
			fontPrint(f,bm,50,80,num,getColor(0,255,0,0),0);				
			//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
			show_font_to_lcd(lcd->mp,265,140,bm);
			//关闭字体，关闭画板
			fontUnload(f);
			destroyBitmap(bm);
			
			ts_open();
			
			int psum=0;
			float sumadd=0.0;
			char psum1[20];
			char sumadd1[20];
			while(1)
			{
				//获取坐标
				ts_read_pos(&ts_x,&ts_y);
				
				
				//点击加号
				if(ts_x>130 && ts_x<250 && ts_y>360 && ts_y<445)
				{
					
					//刷总数
					psum++;
					if(psum>q->num)
					{
						psum=q->num;
					}
					
					//初始化Lcd
					struct LcdDevice* lcd = init_lcd("/dev/fb0");	
					//打开字体	
					font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
					//字体大小的设置
					fontSetSize(f,35);  //字体大小20，可以人为修改
					//创建一个画板（点阵图）宽是200个像素，高是50个像素
					bitmap *bm = createBitmapWithInit(120,80,4,getColor(0,255,255,255)); 
					
					sprintf(psum1,"%d",psum);
					//将字体写到点阵图上 0,0表示汉字在画板的左上角显示			
					fontPrint(f,bm,40,20,psum1,getColor(0,255,0,0),0);				
					//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
					show_font_to_lcd(lcd->mp,270,370,bm);
					//关闭字体，关闭画板
					fontUnload(f);
					destroyBitmap(bm);
					
					
					
					//算总价
					sumadd=(float)psum*(q->price);
					//打开字体	
					font *f1 = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
					//字体大小的设置
					fontSetSize(f1,35);  //字体大小20，可以人为修改
					//创建一个画板（点阵图）宽是200个像素，高是50个像素
					bitmap *bm1 = createBitmapWithInit(190,85,4,getColor(0,255,255,255)); 
					sprintf(sumadd1,"%.1f",sumadd);
					//将字体写到点阵图上 0,0表示汉字在画板的左上角显示			
					fontPrint(f1,bm1,75,22,sumadd1,getColor(0,255,0,0),0);				
					//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
					show_font_to_lcd(lcd->mp,230,255,bm1);
					//关闭字体，关闭画板
					fontUnload(f1);
					destroyBitmap(bm1);
				}
				
				//点击减号
				if(ts_x>400 && ts_x<515 && ts_y>360 && ts_y<445)
				{
					//刷总数
					psum--;
					if(psum<=0)
					{
						psum=0;
					}
					
					//初始化Lcd
					struct LcdDevice* lcd = init_lcd("/dev/fb0");	
					//打开字体	
					font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
					//字体大小的设置
					fontSetSize(f,35);  //字体大小20，可以人为修改
					//创建一个画板（点阵图）宽是200个像素，高是50个像素
					bitmap *bm = createBitmapWithInit(120,80,4,getColor(0,255,255,255)); 
					
					sprintf(psum1,"%d",psum);
					//将字体写到点阵图上 0,0表示汉字在画板的左上角显示			
					fontPrint(f,bm,40,20,psum1,getColor(0,255,0,0),0);				
					//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
					show_font_to_lcd(lcd->mp,270,370,bm);
					//关闭字体，关闭画板
					fontUnload(f);
					destroyBitmap(bm);
					
					
					
					//算总价
					sumadd=(float)psum*(q->price);
					//打开字体	
					font *f1 = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
					//字体大小的设置
					fontSetSize(f1,35);  //字体大小20，可以人为修改
					//创建一个画板（点阵图）宽是200个像素，高是50个像素
					bitmap *bm1 = createBitmapWithInit(190,85,4,getColor(0,255,255,255)); 
					sprintf(sumadd1,"%.1f",sumadd);
					//将字体写到点阵图上 0,0表示汉字在画板的左上角显示			
					fontPrint(f1,bm1,75,22,sumadd1,getColor(0,255,0,0),0);				
					//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
					show_font_to_lcd(lcd->mp,230,255,bm1);
					//关闭字体，关闭画板
					fontUnload(f1);
					destroyBitmap(bm1);
				}
				//点击微信支付
				if(ts_x>596 && ts_x<790 && ts_y>2 && ts_y<150)
				{
					show_anybmp(250,250,245,60,"wechat.bmp");
					sleep(5);
					printf("===========================\n");
					printf("微信支付成功\n");
					//q->num-pnum;链表发生变化
					q->num=q->num-psum;
					FILE *myfile;
					myfile=fopen("2.txt","w+");
					if(myfile==NULL)
					{
						perror("打开文件失败了!\n");
						return -1;
					}
					//此时p,q不指向head，让p重新指回头节点
					p=head;
					//将变动后的链表重新写入记事本
					char message[100]={0};
					while(p->next!=NULL)
					{
						
						p=p->next;
						
						if(p->next==NULL)
						{
							fprintf(myfile,"%s@%.1f@%d@%s",p->name,p->price,p->num,p->picpath);
						}
						else
						{
							fprintf(myfile,"%s@%.1f@%d@%s\n",p->name,p->price,p->num,p->picpath);
						}
					}
					
					fclose(myfile);
					flag=1;
					goto lb2;
				}
				//点击支付宝支付
				if(ts_x>596 && ts_x<790 && ts_y>185 && ts_y<330)
				{
					show_anybmp(250,250,245,60,"zfbpay.bmp");
					sleep(5);
					printf("===========================\n");
					printf("支付宝支付成功\n");
					//q->num-pnum;链表发生变化
					q->num=q->num-psum;
					FILE *myfile;
					myfile=fopen("2.txt","w+");
					if(myfile==NULL)
					{
						perror("打开文件失败了!\n");
						return -1;
					}
					//此时p,q不指向head，让p重新指回头节点
					p=head;
					//将变动后的链表重新写入记事本
					char message[100]={0};
					while(p->next!=NULL)
					{
						
						p=p->next;
						
						if(p->next==NULL)
						{
							fprintf(myfile,"%s@%.1f@%d@%s",p->name,p->price,p->num,p->picpath);
						}
						else
						{
							fprintf(myfile,"%s@%.1f@%d@%s\n",p->name,p->price,p->num,p->picpath);
						}
					}
					
					fclose(myfile);
					flag=1;
					goto lb2;
				}
				
				//点击返回
				if(ts_x>15 && ts_x<134 && ts_y>11 && ts_y<53)
				{
					flag=1;
					goto lb2;
				}
				
			}
			
		}
		lb2:
		if(flag==1)
		{
			break;
		}
				
		//75+160*i,50
		//点击到第一个商品
		if(ts_x>55 && ts_x<135 && ts_y>50 && ts_y<170)
		{
			if(sum-1>=0)
			{
				for(int i=0;i<sum-1;i++)
				{
					q=q->prev;
				}
				goto lb1;
			}
			else
			{
				printf("此处没有商品\n");
			}
		}
		
		//点击到第二个商品
		if(ts_x>215 && ts_x<290 && ts_y>50 && ts_y<170)
		{
			if(sum-2>=0)
			{
				for(int i=0;i<sum-2;i++)
				{
					q=q->prev;
				}
				goto lb1;
			}
			else
			{
				printf("此处没有商品\n");
			}
		}
		
		//点击到第三个商品
		if(ts_x>400 && ts_x<460 && ts_y>50 && ts_y<170)
		{
			if(sum-3>=0)
			{
				for(int i=0;i<sum-3;i++)
				{
					q=q->prev;
				}
				goto lb1;
			}
			else
			{
				printf("此处没有商品\n");
			}
		}
		
		//点击到第四个商品
		if(ts_x>560 && ts_x<620 && ts_y>50 && ts_y<170)
		{
			if(sum-4>=0)
			{
				for(int i=0;i<sum-4;i++)
				{
					q=q->prev;
				}
				goto lb1;
			}
			else
			{
				printf("此处没有商品\n");
			}
		}
		
	}
}


//管理员登录
int login_fun()
{
	char number[10];
	char start[7]="123456";
	//显示管理员登录界面
	show_anybmp(800,480,0,0,"manage.bmp");
	ts_open();
	while(1) //读取触摸屏的坐标，判断你点击是哪个按钮
	{
		ts_read_pos(&ts_x,&ts_y);
		//判断坐标
		//点到密码，输入密码
		if(ts_x>270 && ts_x<560 && ts_y>340 && ts_y<367) 
		{
lb:
			printf("请输入管理员密码\n");
			scanf("%s",number);
			if(strcmp(start,number)==0)
			{
				n=PRODUCE;
				break;
			}
			else
			{
				printf("密码不正确，请重新输入\n");
				bzero(number,10);
				getchar();
				goto lb;
			}
		}
		//返回按钮
		if(ts_x>30 && ts_x<117 && ts_y>44 && ts_y<98)
		{
			n=MAINBUY;
			break;
		}
		
	}
}

//商品管理
int produce()
{
	//初始化链表
	struct doublelist *head=list_init();
	struct doublelist *p=head;
	readlist(head);
	
	show_anybmp(800,480,0,0,"produce.bmp");
	ts_open();
	while(1)
	{
		ts_read_pos(&ts_x,&ts_y);
		//返回
		if(ts_x>662 && ts_x<785 && ts_y>0 && ts_y<70)
		{
			n=MAINBUY;
			break;
		}
		
		//上架按钮
		if(ts_x>6 && ts_x<200 && ts_y>5 && ts_y<110)
		{
			char name[20];
			float price;
			int num;
			char path[20];
			printf("请输入需要上架的货物名称、价格、数量、路径：\n");
			scanf("%s",name);
			scanf("%f",&price);
			scanf("%d",&num);
			scanf("%s",path);
			list_insert_tail(head,name,price,num,path);
			getchar();
			list_show(head);
			
			FILE *myfile;
			myfile=fopen("2.txt","w+");
			if(myfile==NULL)
			{
				perror("打开文件失败了!\n");
				return -1;
			}
			char message[100]={0};
			while(p->next!=NULL)
			{
				
				p=p->next;
				
				if(p->next==NULL)
				{
					fprintf(myfile,"%s@%.1f@%d@%s",p->name,p->price,p->num,p->picpath);
				}
				else
				{
					fprintf(myfile,"%s@%.1f@%d@%s\n",p->name,p->price,p->num,p->picpath);
				}
				
			}
			
			fclose(myfile);
			break;
		}
		
		//下架按钮
		if(ts_x>6 && ts_x<200 && ts_y>123 && ts_y<225)
		{
			char delname[20];
			printf("请输入需要下架货物名称:\n");
			scanf("%s",delname);
			list_del(head,delname);
			getchar();
			list_show(head);

			
			FILE *myfile;
			myfile=fopen("2.txt","w+");
			if(myfile==NULL)
			{
				perror("打开文件失败了!\n");
				return -1;
			}
			char message[100]={0};
			while(p->next!=NULL)
			{
				
				p=p->next;
				
				if(p->next==NULL)
				{
					fprintf(myfile,"%s@%.1f@%d@%s",p->name,p->price,p->num,p->picpath);
				}
				else
				{
					fprintf(myfile,"%s@%.1f@%d@%s\n",p->name,p->price,p->num,p->picpath);
				}
				
			}
			
			fclose(myfile);
			break;
		}
		
		//修改商品按钮
		if(ts_x>6 && ts_x<200 && ts_y>247 && ts_y<331)
		{
			char old[20];
			char newname[20];
			float newprice;
			int newnum;
			char newpath[20];
			
			printf("请输入需要修改的货物名称：\n");
			scanf("%s",old);
			printf("请输入新的货物名称、价格、数量、路径：\n");
			scanf("%s",newname);
			scanf("%f",&newprice);
			scanf("%d",&newnum);
			scanf("%s",newpath);
			list_update(head,old,newname,newprice,newnum,newpath);
			getchar();
			list_show(head);
			
			FILE *myfile;
			myfile=fopen("2.txt","w+");
			if(myfile==NULL)
			{
				perror("打开文件失败了!\n");
				return -1;
			}
			char message[100]={0};
			while(p->next!=NULL)
			{
				
				p=p->next;
				
				if(p->next==NULL)
				{
					fprintf(myfile,"%s@%.1f@%d@%s",p->name,p->price,p->num,p->picpath);
				}
				else
				{
					fprintf(myfile,"%s@%.1f@%d@%s\n",p->name,p->price,p->num,p->picpath);
				}
				
			}
			
			fclose(myfile);
			
			
		}
		
		//查看商品按钮
		if(ts_x>6 && ts_x<200 && ts_y>355 && ts_y<460)
		{
			char checkname[20];
			printf("请输入需要查看的商品名称:\n");
			scanf("%s",checkname);
			printf("======================\n");
			while(p->next!=NULL)
			{
				p=p->next;
				if(strcmp(p->name,checkname)==0)
				{
					break;
				}
			}
			if(p->next==NULL && strcmp(p->name,checkname)!=0)
			{
				printf("没有此商品\n");
			}
			else
			{
				//刷图片
				show_anybmp(75,120,350,100,p->picpath);
				//初始化Lcd
				struct LcdDevice* lcd = init_lcd("/dev/fb0");	
				//打开字体	
				font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
				//字体大小的设置
				fontSetSize(f,20);  //字体大小20，可以人为修改
				//创建一个画板（点阵图）宽是200个像素，高是50个像素
				bitmap *bm = createBitmapWithInit(110,120,4,getColor(0,255,255,255)); 
				char buf1[]="商品：";
				char buf2[]="价格：";
				char buf3[]="数量：";
				char name[50];
				char price[50];
				char num[50];
				sprintf(name,"%s",p->name);
				sprintf(price,"%.1f",p->price);
				sprintf(num,"%d",p->num);
				//将字体写到点阵图上 0,0表示汉字在画板的左上角显示
				fontPrint(f,bm,0,0,buf1,getColor(0,255,0,0),0);
				fontPrint(f,bm,0,40,buf2,getColor(0,255,0,0),0);
				fontPrint(f,bm,0,80,buf3,getColor(0,255,0,0),0);			
				fontPrint(f,bm,50,0,name,getColor(0,255,0,0),0);
				fontPrint(f,bm,50,40,price,getColor(0,255,0,0),0);
				fontPrint(f,bm,50,80,num,getColor(0,255,0,0),0);				
				//把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
				show_font_to_lcd(lcd->mp,350,220,bm);
				//关闭字体，关闭画板
				fontUnload(f);
				destroyBitmap(bm);
				//5秒后消失
				sleep(5);
				show_anybmp(800,480,0,0,"produce.bmp");
			}
			break;
			
		}
		
		
	}	
}

